package modbus

type ModbusTCPAnalysis struct {
	ModbusAnalysis
	header  []uint8
	transID uint16
}

func (_this *ModbusTCPAnalysis) Start() bool {
	_this.unpackLinkLayer = _this.unpackLinkLayerFun
	_this.packLinkLayer = _this.packLinkLayerFun
	_this.transIDAdd = _this.transIDAddFun
	_this.headInit()
	return _this.ModbusAnalysis.Start()
}
func (_this *ModbusTCPAnalysis) transIDAddFun() {
	if _this.transID == 0xffff {
		_this.transID = 0
	} else {
		_this.transID++
	}
}
func (_this *ModbusTCPAnalysis) headInit() {
	if _this.header == nil || len(_this.header) != 6 {
		_this.header = []uint8{0, 0, 0, 0, 0, 0}
	}
}
func (_this *ModbusTCPAnalysis) packLinkLayerFun(buffer []uint8) []uint8 {
	_this.headInit()
	len := uint16(len(buffer) /* + 2*/)
	_this.header[4] = uint8(len >> 8)
	_this.header[5] = uint8(len & 0xff)
	_this.header[0] = uint8(_this.transID >> 8)
	_this.header[1] = uint8(_this.transID & 0xff)
	return append(_this.header, buffer...)
}

func (_this *ModbusTCPAnalysis) unpackLinkLayerFun(buffer []uint8) []uint8 {
	return buffer[6:]
	//if !crc.Crc16Check(buffer) {
	// logger.Info("crc校验失败")
	//	return nil
	//} else {
	//	return buffer[6:]
	//}
}
